package com.oauth2demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oauth2demo.constants.MessageConstant;
import com.oauth2demo.comment.R;
import com.oauth2demo.constants.RedisConstant;
import com.oauth2demo.doman.SecurityUser;
import com.oauth2demo.doman.SysUser;
import com.oauth2demo.mapper.SysUserMapper;
import com.oauth2demo.service.SysUserService;
import com.oauth2demo.utils.JwtUtils;
import com.oauth2demo.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Objects;

/**
 * <h3>oauth2Demo</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2024-07-04 11:06:02
 */
@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUserName, username);
        SysUser sysUser=sysUserMapper.selectOne(wrapper);
        if (Objects.isNull(sysUser)) {
            throw new RuntimeException(MessageConstant.USERNAME_ERROR);
        }
        return new SecurityUser(sysUser);
    }

    /**
     * 登录
     * @param sysUser
     * @return
     */
    @Override
    public R login(SysUser sysUser) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(sysUser.getUserName(), sysUser.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if(Objects.isNull(authenticate)){
            throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
        }
        SecurityUser principal = (SecurityUser)authenticate.getPrincipal();
        SysUser sysuser = principal.getUser();
        sysuser.setPassword(null);
        String userName = sysuser.getUserName();
        String jwt = JwtUtils.createJWT(userName);
        redisUtils.set(RedisConstant.USER_LOGIN +sysuser.getUserName(),sysuser);
        HashMap<String,Object> map=new HashMap<>();
        map.put("token",jwt);
        return R.success(map, MessageConstant.USERNAME_SUCCESS);
    }
}
